﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using WPFExcelReport.Chart;
namespace WPFExcelReport
{
    public partial class TAK02 : ViewBase
    {
        #region ==KSTKRS===========
        public static readonly DependencyProperty KSTKRSProperty = DependencyProperty.Register("KSTKRS",
            typeof(string), typeof(TAK02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKSTKRSChanged));
        public string KSTKRS
        {
            get
            {
                return (string)GetValue(KSTKRSProperty);
            }
            set
            {
                SetValue(KSTKRSProperty, value);
            }
        }
        private static void OnKSTKRSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAK02;
            if (src != null)
            {
                src.KSTKRS = (string)e.NewValue;
            }
        }
        #endregion

        #region ==KST===========
        public static readonly DependencyProperty KSTProperty = DependencyProperty.Register("KST",
            typeof(string), typeof(TAK02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKSTChanged));
        public string KST
        {
            get
            {
                return (string)GetValue(KSTProperty);
            }
            set
            {
                SetValue(KSTProperty, value);
            }
        }
        private static void OnKSTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAK02;
            if (src != null)
            {
                src.KST = (string)e.NewValue;
            }
        }
        #endregion

        #region ==AMTYP===========
        public static readonly DependencyProperty AMTYPProperty = DependencyProperty.Register("AMTYP",
            typeof(string), typeof(TAK02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAMTYPChanged));
        public string AMTYP
        {
            get
            {
                return (string)GetValue(AMTYPProperty);
            }
            set
            {
                SetValue(AMTYPProperty, value);
            }
        }
        private static void OnAMTYPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAK02;
            if (src != null)
            {
                src.AMTYP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==IsSP===========
        public static readonly DependencyProperty IsSPProperty = DependencyProperty.Register("IsSP",
            typeof(bool), typeof(TAK02),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsSPChanged));
        public bool IsSP
        {
            get
            {
                return (bool)GetValue(IsSPProperty);
            }
            set
            {
                SetValue(IsSPProperty, value);
            }
        }
        private static void OnIsSPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAK02;
            if (src != null)
            {
                src.IsSP = (bool)e.NewValue;
            }
        }

        #endregion
        #region ==IsHN===========
        public static readonly DependencyProperty IsHNProperty = DependencyProperty.Register("IsHN",
            typeof(bool), typeof(TAK02),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsHNChanged));
        public bool IsHN
        {
            get
            {
                return (bool)GetValue(IsHNProperty);
            }
            set
            {
                SetValue(IsHNProperty, value);
            }
        }
        private static void OnIsHNChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAK02;
            if (src != null)
            {
                src.IsHN = (bool)e.NewValue;
            }
        }

        #endregion
        #region ==IsPT===========
        public static readonly DependencyProperty IsPTProperty = DependencyProperty.Register("IsPT",
            typeof(bool), typeof(TAK02),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsPTChanged));
        public bool IsPT
        {
            get
            {
                return (bool)GetValue(IsPTProperty);
            }
            set
            {
                SetValue(IsPTProperty, value);
            }
        }
        private static void OnIsPTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAK02;
            if (src != null)
            {
                src.IsPT = (bool)e.NewValue;
            }
        }

        #endregion
        #region ==IsSD===========
        public static readonly DependencyProperty IsSDProperty = DependencyProperty.Register("IsSD",
            typeof(bool), typeof(TAK02),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsSDChanged));
        public bool IsSD
        {
            get
            {
                return (bool)GetValue(IsSDProperty);
            }
            set
            {
                SetValue(IsSDProperty, value);
            }
        }
        private static void OnIsSDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAK02;
            if (src != null)
            {
                src.IsSD = (bool)e.NewValue;
            }
        }

        #endregion
        #region ==IsSZ===========
        public static readonly DependencyProperty IsSZProperty = DependencyProperty.Register("IsSZ",
            typeof(bool), typeof(TAK02),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsSZChanged));
        public bool IsSZ
        {
            get
            {
                return (bool)GetValue(IsSZProperty);
            }
            set
            {
                SetValue(IsSZProperty, value);
            }
        }
        private static void OnIsSZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAK02;
            if (src != null)
            {
                src.IsSZ = (bool)e.NewValue;
            }
        }

        #endregion
        #region ==IsZP===========
        public static readonly DependencyProperty IsZPProperty = DependencyProperty.Register("IsZP",
            typeof(bool), typeof(TAK02),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsZPChanged));
        public bool IsZP
        {
            get
            {
                return (bool)GetValue(IsZPProperty);
            }
            set
            {
                SetValue(IsZPProperty, value);
            }
        }
        private static void OnIsZPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAK02;
            if (src != null)
            {
                src.IsZP = (bool)e.NewValue;
            }
        }

        #endregion

        public TAK02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_STDMACHINE();
            MESSAGES += "STAK02;";
            DataContext = MV;
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            ResetChart(null);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        }
        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "STAK02":
                    KSTKRS = msg.KRS;
                    KST = msg.LFD;
                    AMTYP = msg.P2;
                    ResetChart(msg);
                    break;
            }
        }
      
        DataSet SResult;
        DataRow dwData;
        public void SetUpChart07(MessageInfo msg)
        {
            ChartEx0007 Chart07;
           
            dwData = TBMain.NewRow();
            dwData["AMKSTKRS"] = KSTKRS;
            dwData["AMKST"] = KST;
            dwData["AMTYP"] = AMTYP;
            SResult = ((VICC_STDMACHINE)MV).QueryICC_STDMACHINE(dwData, 2);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["ABFAGANG"] = Helpers.Helper.GetGB2312Value(row["ABFAGANG"].ToString().Replace("//", "_"));
            }
            MergeData(SResult, true, true);
            /*dwData["ABFSOLLDAT"] = row["GABBUCHDAT"];
            dwData["ABFSOLLMG"] = row["GABKG"]*/
            ;
            int idx = 1;
            string Y = "ABFSOLLDAT";
            string X = "ABFSOLLMG";
            string name = "";
            //ABFAGANG：归类。Chart05.SetUp自动将日期累加

            var query = (from DataRow row in TBMain.AsEnumerable()
                         orderby row.Field<string>("ABFAGANG")
                         select row.Field<string>("ABFAGANG")).Distinct();
            foreach (var str in query)
            {
              
                name = str.Trim();
                X = "ABFSOLLDAT";
                Y = "ABFSOLLMG";
                string sort = " ABFSOLLDAT ASC";
                DataRow[] rows = TBMain.Select("ABFAGANG='" + str + "'", sort);
                DataTable temp = rows.CopyToDataTable();
                if (rows.Length == 0)
                    continue;
                Chart07 = null;
                switch (rows[0]["SMTYP"].ToString().Trim())
                { 
                    case "SP":
                        IsSP = true;
                        Chart07 = chSP;
                        break;
                    case "HN":
                        IsHN = true;
                        Chart07 = chHN;
                        break;
                    case "PT":
                        IsPT = true;
                        Chart07 = chPT;
                        break;
                    case "SD":
                        IsSD = true;
                        Chart07 = chSD;
                        break;
                    case "SZ":
                        IsSZ = true;
                        Chart07 = chSZ;
                        break;
                    case "ZP":
                        IsZP = true;
                        Chart07 = chZP;
                        break;
                }
                if (Chart07 == null)
                    Chart07 = new ChartEx0007();
                Chart07.SetChartSleep();
                Chart07.RATIO = 1;
                Chart07.Converter = 1;
                Chart07.SetUp(idx, name, temp, X, Y);
                idx++; 
                Chart07.SetChartGetUp(); 
            }
          
        }

        public void ResetChart(MessageInfo msg)
        {  
            TBMain.Clear(); 
            SetUpChart07(msg);
        }
      
    }
}